home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / rayshade / graphtal.lzh / Graphtal.Amiga / Module.C < prev    next >
C/C++ Source or Header  |  1992-11-17  |  3KB  |  118 lines

  1. /*
  2.  * Module.C - methods for L-System module handling.
  3.  *
  4.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  5.  *                     University of Berne, Switzerland
  6.  * All rights reserved.
  7.  *
  8.  * This software may be freely copied, modified, and redistributed
  9.  * provided that this copyright notice is preserved on all copies.
  10.  *
  11.  * You may not distribute this software, in whole or in part, as part of
  12.  * any commercial product without the express consent of the authors.
  13.  *
  14.  * There is no warranty or other guarantee of fitness of this software
  15.  * for any purpose.  It is provided solely "as is".
  16.  *
  17.  */
  18.  
  19. #include "Module.h"
  20. #include "Error.h"
  21.  
  22. implementList(ExpressionList, ExpressionPtr);
  23. implementList(ValueList, ValuePtr);
  24. implementList(ModuleList, ModulePtr);
  25. implementList(ProdModuleList, ProdModulePtr);
  26.  
  27. //___________________________________________________________ Formals
  28.  
  29. const int MAX_PARAMETERS = 100;
  30. static Value Parameters[100];
  31.  
  32. Value* Formals(int i)
  33. {
  34.   if (i>=MAX_PARAMETERS)
  35.     Error(ERR_PANIC, "Formals: index out of range");
  36.   return &Parameters[i];
  37. }
  38.  
  39. //___________________________________________________________ ProdModule
  40.  
  41. ProdModule::ProdModule(const Name& n, ExpressionList* expressions)
  42. : name(n), parameters(expressions)
  43. {
  44.   extern unsigned int modHash(const char*, long);
  45.   if (expressions)
  46.     hashValue = modHash((const char*) n, expressions->count());
  47.   else
  48.     hashValue = modHash((const char*) n, 0L);
  49. }
  50.  
  51. ProdModule::~ProdModule()
  52. {
  53.   if (parameters) {
  54.     for (long i=0; i<parameters->count(); i++)
  55.       delete parameters->item(i);
  56.     delete parameters;
  57.   }
  58. }
  59.  
  60. ostream& operator<<(ostream& os, const ProdModule& m)
  61. {
  62.   os << m.name;
  63.   if (m.parameters) {
  64.     os << '(';
  65.     for (long i=0; i<m.parameters->count()-1; i++)
  66.       os << *m.parameters->item(i) << ", ";    
  67.     os << *m.parameters->item(i) << ')';
  68.   }
  69.   return os;
  70. }
  71.  
  72. //___________________________________________________________ Module
  73.  
  74. // Evaluate the parameter of the ProdModule and store the results
  75. //  as the constant arguments of the module
  76. Module::Module(ProdModule* m)
  77. : _name(m->name), _hashValue(m->hashValue)
  78. {
  79.   if (m->parameters) {
  80.     parameters = new ValueList(m->parameters->count());
  81.     for (register long i=0; i<m->parameters->count(); i++)
  82.       parameters->append(new Value(m->parameters->item(i)->evaluate()));
  83.   }
  84.   else
  85.     parameters = NULL;
  86. }
  87.  
  88. Module::~Module()
  89. {
  90.   if (parameters) {
  91.     for (long i=0; i<parameters->count(); i++)
  92.       delete parameters->item(i);
  93.     delete parameters;
  94.   }
  95. }
  96.  
  97. // Binding is done by assigning the actual parameters of the module
  98. //  to the Formals-array. This works because all the variables of the
  99. //  productions points to this array!
  100. void Module::bind()
  101. {
  102.   if (parameters)
  103.     for (register long i=0; i<parameters->count(); i++)
  104.       Parameters[i] = *parameters->item(i);
  105. }
  106.  
  107. ostream& operator<<(ostream& os, const Module& m)
  108. {
  109.   os << m._name;
  110.   if (m.parameters) {
  111.     os << '(';
  112.     for (long i=0; i<m.parameters->count()-1; i++)
  113.       os << *m.parameters->item(i) << ", ";
  114.     os << *m.parameters->item(i) << ')';
  115.   }
  116.   return os;
  117. }
  118.